package com.astn.adapter.booking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.astn.bks.PMyOffer;
import com.astn.bks.R;
import com.astn.bks.util.DateUtils;
import com.astn.bks.util.JsonUtils;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.CRequestHolder;
import com.astn.entity.booking.POfferHolder;
import com.astn.entity.booking.QuoteOffer;
import com.astn.entity.booking.QuoteRequest;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class POfferAdapter extends BaseAdapter {
	private ArrayList<QuoteOffer> lstItems = new ArrayList<QuoteOffer>();
	private Context context;
	LayoutInflater oInflater;

	public POfferAdapter(Context context, ArrayList<QuoteOffer> lstItems) {
		this.context = context;
		this.lstItems = lstItems;
		oInflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lstItems.size();
	}

	@Override
	public QuoteOffer getItem(int position) {
		// TODO Auto-generated method stub
		return lstItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return lstItems.get(position).getId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		POfferHolder holder;
		if (convertView == null) {
			convertView = oInflater.inflate(R.layout.pmyofferitem, null);
			holder = new POfferHolder();
			holder.txvPerson = (TextView) convertView
					.findViewById(R.id.txvPerson);
			holder.txvDestination = (TextView) convertView
					.findViewById(R.id.txvDestination);
			holder.txvTime = (TextView) convertView.findViewById(R.id.txvTime);
			holder.btnView = (ImageButton) convertView
					.findViewById(R.id.btvDetail);
			holder.layStatus = (LinearLayout) convertView
					.findViewById(R.id.layStatus);

			// holder.status.
			convertView.setTag(holder);
		} else {
			holder = (POfferHolder) convertView.getTag();
		}
		String path = "";
		String objStr = "";
		try {
			Map<String, Object> mapPara = new HashMap<String, Object>();
			mapPara.put("REQUESTID", lstItems.get(position).getRequest_id());
			objStr = (String) WebserviceUtils.call("com_xmlrpc",
					"getDestinationInfoOfRequest", mapPara);
		} catch (Exception e) {
			// TODO: handle exception
		}
		JsonObject rsObj = null;
		if (objStr.length() > 0) {
			try {
				JsonParser parser = new JsonParser();
				rsObj = (JsonObject) parser.parse(objStr);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		if (rsObj != null) {
			if (!rsObj.get("cityname").isJsonNull()) {
				if (rsObj.get("cityname").getAsString().trim().length() > 0) {
					path += rsObj.get("cityname").getAsString() + "-";
				}
			}
			if (!rsObj.get("statename").isJsonNull()) {
				if (rsObj.get("statename").getAsString().trim().length() > 0) {
					path += rsObj.get("statename").getAsString() + "-";
				}
			}
		}
		holder.status = new ImageView(convertView.getContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		params.gravity = Gravity.RIGHT;
		params.leftMargin = 5;
		holder.status.setLayoutParams(params);
		holder.status.setBackgroundResource(R.color.bodybackground);
		holder.status.setScaleType(ScaleType.CENTER);
		path += rsObj.get("countryname").getAsString();
		holder.txvDestination.setText(path);
		String time = "From: "
				+ DateUtils.quoteDateFormat.format(lstItems.get(position)
						.getFrom())
				+ "-To: "
				+ DateUtils.quoteDateFormat.format(lstItems.get(position)
						.getTo());
		String person = "Adult: "
				+ String.valueOf(lstItems.get(position).getAdult())
				+ ", children: "
				+ String.valueOf(lstItems.get(position).getChildren());
		holder.txvPerson.setText(person);
		holder.txvTime.setText(time);
		if (lstItems.get(position).getAccept_offer() > 0) {
			if (lstItems.get(position).getAccept_offer() == lstItems.get(
					position).getId()) {
				holder.status.setImageResource(R.drawable.win);
			} else {
				holder.status.setImageResource(R.drawable.icon_fail);
			}
			holder.layStatus.addView(holder.status);
		}
		holder.btnView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(
						context,
						String.valueOf("Offer: "
								+ lstItems.get(position).getId()),
						Toast.LENGTH_SHORT).show();
			}

		});

		// holder.txvInfo.setText(lstItems.get(position).getCountryid());
		return convertView;
	}
}
